package com.nit.lab.service.Impl;

import com.nit.lab.dao.FileDao;
import com.nit.lab.dto.DtoStore;
import com.nit.lab.dto.utils.DtoDataTable;
import com.nit.lab.dto.DtoSysFile;
import com.nit.lab.model.SysFileEntity;
import com.nit.lab.service.FileService;
import com.nit.lab.utils.SystemConstants;
import com.nit.lab.utils.UploadUtie;
import javafx.scene.input.DataFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.out;

/**
 *
 * @author be_yond
 * @date 2017/10/25
 */
@Service
public class FileServiceImpl implements FileService{
    @Resource
    private FileDao fileDao;

    @Override
    public Integer getFile4DataTableCount(String search,Integer pre,Integer actorId) throws Exception {
        Integer result = 0;
        try {
            result = this.fileDao.getFile4DataTableCount(search,pre,actorId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<DtoSysFile> getFileDataTable(DtoDataTable dtoDataTable,Integer pre,Integer actorId) throws Exception {
        List<DtoSysFile> result = new ArrayList<DtoSysFile>();
        result = fileDao.getFileDataTable(dtoDataTable.getStart(), dtoDataTable.getLength(), dtoDataTable.getSearch(),pre,actorId);
        return result;
    }

    @Override
    public Integer uploadFile(SysFileEntity sysFileEntity) throws Exception {
        sysFileEntity.setUploadTime(UploadUtie.getDate());
        sysFileEntity.setIsDelete(0);
        sysFileEntity.setKeyWord(" ");
        return fileDao.uploadFile(sysFileEntity);
    }

    @Override
    public Integer updataFileInfo(SysFileEntity sysFileEntity) throws Exception {
        Integer result = SystemConstants.Update_Success;
        try {
            result = fileDao.updataFileInfo(sysFileEntity);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String deleteFile(Integer fileId) throws Exception {
        return fileDao.deleteFile(fileId);
    }

    @Override
    public List<DtoSysFile> getFileDataTableForUser(DtoDataTable dtoDataTable,Integer sessionId) throws Exception {
        List<DtoSysFile> result = new ArrayList<DtoSysFile>();
        result = fileDao.getFileDataTableForUser(dtoDataTable.getStart(), dtoDataTable.getLength(), dtoDataTable.getSearch(),sessionId);
        return result;
    }

    @Override
    public Integer getFile4DataTableForUserCount(String search,Integer sessionId) throws Exception {
        Integer result = 0;
        try {
            result = fileDao.getFile4DataTableForUserCount(search,sessionId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<DtoStore> getAllStore() throws Exception {
        List<DtoStore> dtoStores = new ArrayList<DtoStore>();
        dtoStores = fileDao.dtoStores();
        return dtoStores;
    }

}
